package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import Model.Snake;
import View.GamePanel;

public class MainFrame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private GamePanel gamePanel;
	private Snake snake;
	public MainFrame() {
		setBounds(1200, 100, 600, 550);
		setVisible(true);
		setTitle("Snake");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		String[] menus={"游戏","速度","帮助"};
		String [][] items={{"新游戏","暂停","继续","退出"},{"快速","中速","慢速"},{"关于"}};
		JMenuBar mb=new JMenuBar();
		for(int i=0;i<menus.length;i++){
			JMenu m=new JMenu(menus[i]);
			for(int j=0;j<items[i].length;j++){
				JMenuItem item=new JMenuItem(items[i][j]);
				item.addActionListener(this);
			m.add(item)	;
			}
			mb.add(m);	
		}
		setJMenuBar(mb);
		setLayout(null);
		setResizable(false);
		
		snake=new Snake();
		gamePanel=new GamePanel();
		gamePanel.setLocation(0, 0);
		add(gamePanel);
	}

	public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
	
	@Override
	public void run() {
		new MainFrame();
		
	}
});
	}
	public void actionPerformed(ActionEvent e) {
		String cmd=e.getActionCommand();
		switch (cmd) {
		case "新游戏":
			snake.addObserver(gamePanel);
			snake.init();						
			break;
		}
	}
}